package cn.aylog.jdbc;

import cn.aylog.pojo.User;
import cn.aylog.utils.AssetType;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;

public class Session {
    Properties cfg = new Properties();
    void loadConfig() {
        String path = "db.properties";
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(path);
        try {
            cfg.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    List<Object> selset(Object params) throws Exception {
        String driver = cfg.getProperty("db.driver");
        String url = cfg.getProperty("db.url");
        String username = cfg.getProperty("db.username");
        String password = cfg.getProperty("db.password");

        // 驱动加载
        Class.forName(driver);

        // 获取连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 准备sql
        String sql = cfg.getProperty("db.sql");
        PreparedStatement stmt = conn.prepareStatement(sql);

        //参数回填
        if(AssetType.isBaseType(params)) {
            stmt.setObject(1,params);
        } else {
            // 获取入参类型
            String paramType = cfg.getProperty("db.paramType");
            Class<?> paramClass= Class.forName(paramType);
            // 获取入参字段-值
            String paramStr = cfg.getProperty("db.params");
            String[] paramsName = paramStr.split(",");
            //参数设置
            for (int i=0; i<paramsName.length; i++) {
                Field field = paramClass.getDeclaredField(paramsName[i]);
                field.setAccessible(true);
                Object value = field.get(params);
                stmt.setObject(i+1, value);
            }
        }
        List<Object> list = new ArrayList<>();
        Object obj = null;
        // 获取结果参数类型
        String resultType = cfg.getProperty("db.resultType");
        Class<?> resultTypeClass = Class.forName(resultType);
        // 获取结果
        ResultSet rs = stmt.executeQuery();
        while(rs.next()) {
            obj = resultTypeClass.newInstance();
            // 获取返回值的列信息
            ResultSetMetaData metaData = rs.getMetaData();
            int count = metaData.getColumnCount();
            for(int i=1; i<=count; i++) {
                String columnName = metaData.getColumnName(i);
                Field field = resultTypeClass.getDeclaredField(columnName);
                field.setAccessible(true);
                Object value = rs.getObject(columnName);
                field.set(obj, value);
            }
            list.add(obj);
        }
        rs.close();
        stmt.close();
        conn.close();
        return list;
    }

    public static void main(String[] args) throws Exception {
        Session session = new Session();
        session.loadConfig();
        User user = new User();
        user.setId(6);
        List<Object> list = session.selset(user);
        System.out.println(list);


    }
}
